package com.kl.modular.sysman.system.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kl.modular.common.controller.BaseController;
import com.kl.modular.common.response.ResponseBase;
import com.kl.modular.sysman.system.service.api.ISystemAreaCacheService;
import com.kl.modular.system.dao.model.*;
import com.kl.modular.system.service.api.*;
import com.kl.modular.system.service.bo.SystemDicTypeBo;
import com.kl.modular.system.service.bo.SystemParamBo;
import com.kl.modular.system.service.bo.SystemParamTypeBo;
import org.apache.commons.lang.StringUtils;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.plugins.Page;

/**
 * 系统参数控制器
 * @author xr_zjb
 * @date 2017-08-09 13:48
 * @(C) xinrui Copyright 
 */
@Controller
public class SystemParamController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemParamController.class);
	
	@Autowired
	private ISystemParamService systemParamService;

	@Autowired
	private ISystemParamTypeService systemParamTypeService;

	@Autowired
	private ISystemDicTypeService systemDicTypeService;

	@Autowired
	private ISystemDicService systemDicService;

	@Autowired
	private ISystemAreaCacheService systemAreaCacheService;

	@Autowired
	private ISystemAreaService systemAreaService;

	/** 系统参数 view */
	private static String pathSystemParamRoot="/sysman/system/param/";
	
	
	/**
	 * 系统参数列表页面
	 */
	@RequestMapping(value = "/sysman/systemParam/systemParamList", method = RequestMethod.GET)
	@RequiresPermissions("sysman:systemParam_man:view")
	public String queryList(HttpServletRequest request) {
		logger.info("系统参数列表页面");
		SystemParamBo systemParamBo = new SystemParamBo();
		// 查找行政区域数据
		SystemArea systemArea = new SystemArea();
		systemArea.setParentid(1L);
		List<SystemArea> systemAreaList = systemAreaService.selectListByEntity(systemArea);
		request.setAttribute("systemAreaList", JSON.toJSONString(systemAreaList));
		List<SystemParamType> systemParamTypeList = systemParamTypeService.selectListByBo(systemParamBo);
		request.setAttribute("systemParamTypeList", systemParamTypeList);
		SystemDicTypeBo systemDicTypeBo = new SystemDicTypeBo();
		List<SystemDicType> systemDicTypeList = systemDicTypeService.selectListByBo(systemDicTypeBo);
		request.setAttribute("systemDicTypeList", systemDicTypeList);
		return pathSystemParamRoot + "systemParamList.ftl";
	}

	/**
	 * 系统参数列表数据
	 */
	@RequestMapping(value = "/sysman/systemParam/systemParamAjaxList", method = RequestMethod.POST)
	@RequiresPermissions("sysman:systemParam_man:view")
	public String queryAjaxList(SystemParamBo systemParamBo, Page<SystemParam> page, HttpServletRequest request) {
		logger.info("systemParam列表数据>>data:" + JSON.toJSONString(systemParamBo));
		page = systemParamService.selParamPagerByBo(page, systemParamBo);
		request.setAttribute("pager", page);
		return pathSystemParamRoot + "systemParamAjaxList.ftl";
	}
	
	
	/**
	 * 系统参数添加页面
	 * @return
	 */
	@RequestMapping(value="/sysman/systemParam/toSystemParamAdd" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:systemParam_man:add")
	public String toAddSystemParam(Long areaId, HttpServletRequest request){
		logger.info("系统参数添加页面");
		SystemParamTypeBo paramTypeBo = new SystemParamTypeBo();
		List<SystemParamType> paramTypeList = systemParamTypeService.selectListByBo(paramTypeBo);

		SystemDicTypeBo systemDicTypeBo = new SystemDicTypeBo();
		List<SystemDicType> dicTypeList = systemDicTypeService.selectListByBo(systemDicTypeBo);
		SystemArea systemArea = systemAreaCacheService.findAreaById(areaId);
		request.setAttribute("systemArea", systemArea);
		request.setAttribute("dicTypeList", dicTypeList);
		request.setAttribute("paramTypeList", paramTypeList);
		return pathSystemParamRoot + "addSystemParam.ftl";
	}
	
	/**
	 * 添加系统参数
	 */
	@RequestMapping(value="/sysman/systemParam/addSystemParam" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemParam_man:add")
	public String addSystemParam(@Valid SystemParam systemParam, BindingResult result, HttpServletResponse response) throws Exception{
		logger.info("添加系统参数>>data:" + JSON.toJSONString(systemParam));

		if (result.hasErrors()) {
			return ResponseBase.errorStr(result.getAllErrors().get(0).getDefaultMessage());
		}

		systemParam.setCreateTime(new Date());
		systemParam.setModifyTime(new Date());
		systemParamService.insert(systemParam);

		String jsonstr = ResponseBase.success();
		logger.info("添加系统参数>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	
	/**
	 * 系统参数更新页面
	 * @return
	 */
	@RequestMapping(value = "/sysman/systemParam/toChangeView" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:systemParam_man:update")
	public String toChangeSystemParam(Long id, HttpServletRequest request) throws Exception{
		logger.info("系统参数修改页面>>data:" + id);
		SystemParam systemParam = systemParamService.selectById(id);
		SystemParamTypeBo paramTypeBo = new SystemParamTypeBo();
		List<SystemParamType> paramTypeList = systemParamTypeService.selectListByBo(paramTypeBo);SystemDicTypeBo systemDicTypeBo = new SystemDicTypeBo();
		List<SystemDicType> dicTypeList = systemDicTypeService.selectListByBo(systemDicTypeBo);
        SystemDic dic = systemDicService.selectById(systemParam.getDicId());
		SystemArea systemArea = systemAreaCacheService.findAreaById(systemParam.getAreaId());
		request.setAttribute("systemArea", systemArea);
		request.setAttribute("dicTypeList", dicTypeList);
		request.setAttribute("paramTypeList", paramTypeList);
		request.setAttribute("systemParam", systemParam);
		request.setAttribute("dic", dic);
		return pathSystemParamRoot + "systemParamUpChange.ftl";
	}

	
	/**	
	 * 修改
	 */
	@RequestMapping(value = "/sysman/systemParam/upChange" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemParam_man:update")
	public String upChangeSystemParam(@Valid SystemParam systemParam, BindingResult result, HttpServletResponse response) throws Exception{
		logger.info("更新系统参数信息>>data:" + JSON.toJSONString(systemParam));

		if (result.hasErrors()) {
			return ResponseBase.errorStr(result.getAllErrors().get(0).getDefaultMessage());
		}
		if(systemParam.getId() == null){
			return ResponseBase.errorStr(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
		}
		systemParam.setModifyTime(new Date());
		systemParamService.updateById(systemParam);

		String jsonstr = ResponseBase.success();
		logger.info("编辑系统参数信息>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	/**
	 * 批量删除系统参数
	 */
	@RequestMapping(value = "/sysman/systemParam/delSystemParam" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:systemParam_man:delete")
	public String delSystemParam(String id,HttpServletResponse response) throws Exception{
		logger.info("批量删除系统参数>>data:" + JSON.toJSONString(id));

		// 数据验证
		if (StringUtils.isEmpty(id)) {
			return ResponseBase.errorStr(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
		}

		String[] codestrs = id.split(",");

		systemParamService.logicalDeleteBatch(codestrs);

		String jsonstr = ResponseBase.success();
		logger.info("删除系统参数资源>>返回view>>data:" + jsonstr);
		return jsonstr;
	}
}
